<template>
  <el-config-provider :locale="zhCn">
    <el-container class="app-container">
      <el-header v-if="isLoggedIn">
        <div class="header-content">
          <div class="logo">健康管理系统</div>
          <div class="user-info">
            <el-dropdown>
              <span class="user-name">{{ userInfo.username || '管理员' }}</span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px" v-if="isLoggedIn">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical"
            router
          >
            <el-sub-menu index="1">
              <template #title>
                <el-icon><User /></el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/users">用户列表</el-menu-item>
              <el-menu-item index="/user-groups">用户分组</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon><Document /></el-icon>
                <span>内容管理</span>
              </template>
              <el-menu-item index="/articles">健康文章</el-menu-item>
              <el-menu-item index="/videos">教学视频</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon><Bell /></el-icon>
                <span>提醒管理</span>
              </template>
              <el-menu-item index="/reminder-templates">提醒模板</el-menu-item>
              <el-menu-item index="/trigger-rules">触发规则</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon><DataAnalysis /></el-icon>
                <span>数据看板</span>
              </template>
              <el-menu-item index="/dashboard">统计概览</el-menu-item>
              <el-menu-item index="/user-analytics">用户分析</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="5">
              <template #title>
                <el-icon><Setting /></el-icon>
                <span>系统设置</span>
              </template>
              <el-menu-item index="/system-settings">系统配置</el-menu-item>
              <el-menu-item index="/logs">日志查看</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view v-if="isLoggedIn" />
          <LoginView v-else />
        </el-main>
      </el-container>
    </el-container>
  </el-config-provider>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { User, Document, Bell, DataAnalysis, Setting } from '@element-plus/icons-vue'
import LoginView from './views/LoginView.vue'

const router = useRouter()
const isLoggedIn = ref(false)
const userInfo = ref({})

// 计算当前激活的菜单
const activeMenu = computed(() => {
  const path = router.currentRoute.value.path
  return path || '/dashboard'
})

// 处理退出登录
const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  isLoggedIn.value = false
  userInfo.value = {}
  router.push('/login')
}

// 检查登录状态
onMounted(() => {
  const token = localStorage.getItem('token')
  const storedUserInfo = localStorage.getItem('userInfo')
  if (token && storedUserInfo) {
    isLoggedIn.value = true
    userInfo.value = JSON.parse(storedUserInfo)
    // 如果当前在登录页，重定向到首页
    if (router.currentRoute.value.path === '/login') {
      router.push('/dashboard')
    }
  } else {
    // 如果没有登录信息，重定向到登录页（除了登录页本身）
    if (router.currentRoute.value.path !== '/login') {
      router.push('/login')
    }
  }
})
</script>

<style scoped>
.app-container {
  height: 100vh;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 100%;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  color: #fff;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-name {
  color: #fff;
  cursor: pointer;
}

.el-header {
  background-color: #1890ff;
  color: #fff;
  padding: 0;
}

.el-aside {
  background-color: #545c64;
}

.el-menu-vertical {
  border-right: none;
  height: 100%;
}
</style>